package com.cloud.logger.zchain.core.common.base;

import lombok.Data;

/**
 * User: wcy
 * Date: 2017/10/18
 * Time: 10:44
 */
@Data
public class ResultMessage<T> {
    private static final Long SUCCESS_CODE = 1L;
    private static final String SUCCESS_MESSAGE = "success";
    /**
     * 是否成功
     */
    private Boolean isSuccess = true;
    // 状态码：1成功，其他为失败
    public long code = SUCCESS_CODE;

    // 成功为success，其他为失败原因
    public String message = SUCCESS_MESSAGE;

    // 数据结果集
    public T content;

    //返回id
    public String id;

    public ResultMessage() {
    }

    public ResultMessage(T content) {
        this.content = content;
    }

    public ResultMessage(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.content = content;
    }

    public static final class Builder<T> {
        // 状态码：1成功，其他为失败
        public long code = SUCCESS_CODE;
        // 成功为success，其他为失败原因
        public String message = SUCCESS_MESSAGE;
        // 数据结果集
        public T content;
        //返回id
        public String id;
        private Boolean isSuccess = true;

        private Builder() {
        }

        public static Builder builder() {
            return new Builder();
        }

        public Builder withIsSuccess(Boolean isSuccess) {
            this.isSuccess = isSuccess;
            return this;
        }

        public Builder withCode(long code) {
            this.code = code;
            return this;
        }

        public Builder withMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder withData(T content) {
            this.content = content;
            return this;
        }

        public Builder withId(String id) {
            this.id = id;
            return this;
        }

        public ResultMessage build() {
            ResultMessage resultMessage = new ResultMessage();
            resultMessage.setIsSuccess(isSuccess);
            resultMessage.setCode(code);
            resultMessage.setMessage(message);
            resultMessage.setContent(content);
            resultMessage.setId(id);
            return resultMessage;
        }
    }
}
